package com.eschool.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

import com.eschool.dao.BranchDao;
import com.eschool.dao.ForumDao;
import com.eschool.dao.RoleDao;
import com.eschool.dao.SchoolDao;
import com.eschool.dao.UserDao;
import com.eschool.model.Branch;
import com.eschool.model.ForumPost;
import com.eschool.model.SaasAdmin;
import com.eschool.model.School;
import com.eschool.model.User;
import com.eschool.service.AlertService;
import com.eschool.service.CustomerService;

public class CustomerServiceImpl implements CustomerService {

	@Override
	public boolean addCustomer(School customer) {

		SaasAdmin admin = (SaasAdmin) ((HttpServletRequest) FacesContext
				.getCurrentInstance().getExternalContext().getRequest())
				.getSession().getAttribute("admin");

		customer.setCreator(admin);
		customer.setAccountStatus(true);
		customer.setCreationDate(new Date());

		if (SchoolDao.addSchool(customer))
			return true;

		return false;
		/*
		 * admin.setRole(RoleDao.getRoleByProperty("roleId", 1));
		 * admin.setBranch(BranchDao.getBranchByProperty("branchId", 1)); if
		 * (UserDao.addUser(admin) != -1) message+= " admin added";
		 */
		// return message;
	}

	@Override
	public boolean addBranch(Branch branch, User admin) {

		if ((UserDao.getUserByProperty("email", admin.getEmail())) != null) {
			return false;
		}

		int branchId = BranchDao.addBranch(branch);

		if (branchId == -1)
			return false;

		admin.setRole(RoleDao.getRoleByProperty("roleId", 2));
		branch.setBranchId(branchId);
		admin.setBranch(branch);

		AlertService service = new AlertServiceImpl();

		List<User> rec = new ArrayList<User>();
		rec.add(admin);

		String message = "Your school '" + branch.getSchool().getSchoolName()
				+ " - " + branch.getBranchTitle()
				+ "' has been registered at JSMSC.";
		message += "\nYour account has been created with following credentials.";
		message += "\n\tEmail:\t " + admin.getEmail();
		message += "\n\tPasword: " + admin.getPassword();
		message += "\n\n\tUse "
				+ "http://localhost:8080/eSchool/faces/pages/customer/login.xhtml"
				+ " to login ";

		service.sendAlert("Registration with JSMSC", message, rec, null);

		// student.getParent().setParentId(parentId);

		if (UserDao.addUser(admin) == -1)
			return false;

		return true;
	}

	@Override
	public boolean rechargeCustomer(School customer) {
		SaasAdmin admin = (SaasAdmin) ((HttpServletRequest) FacesContext
				.getCurrentInstance().getExternalContext().getRequest())
				.getSession().getAttribute("admin");
		SchoolDao.insertHistory(customer, admin, new Date());
		return SchoolDao.recharge(customer);
	}

	@Override
	public boolean updateUser(User user) {
		return UserDao.updateUser(user);
	}

	@Override
	public boolean updateCustomerStatus(List<School> customers,
			boolean newStatus) {
		for (Iterator iterator = customers.iterator(); iterator.hasNext();) {
			School customer = (School) iterator.next();
			customer.setAccountStatus(newStatus);
			SchoolDao.updateStatus(customer);
		}

		return true;
	}

	@Override
	public List getForumPosts(int branchId) {
		List posts = ForumDao.getParentPosts(branchId);
		return posts;
	}

	@Override
	public ForumPost getParentPost(int parentId) {
		// TODO Auto-generated method stub
		ForumPost parentPost = ForumDao.getPostByProperty("postId", parentId);
		return parentPost;
	}

	@Override
	public List<ForumPost> getAllChildPosts(int parentId) {
		// TODO Auto-generated method stub
		Map<String, Object> conditions = new HashMap<String, Object>();
		conditions.put("parentPost",
				ForumDao.getPostByProperty("postId", parentId));
		List<ForumPost> childPosts = ForumDao.getPosts(conditions);
		return childPosts;
	}

	@Override
	public boolean addForumPost(ForumPost post) {
		// TODO Auto-generated method stub
		return ForumDao.addPost(post);
	}
}
